package com.qgz.framework.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;

import com.qgz.framework.web.config.ActionConfig;
import com.qgz.framework.web.config.ActionContainer;

public class WebControl extends VelocityServlet {
	Logger log = Logger.getLogger(WebControl.class);
	private static String realPath;

	private static final String TAG_WEB_CONFIG = "webconfig";
	
	private static final String TAG_ENCODE	="encode";
	

	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		realPath = config.getServletContext().getRealPath("/");

		Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, realPath);

		webConfigInit(config);
		
		//setEncodeType(config);
	}

	protected Template handleRequest(HttpServletRequest request,
			HttpServletResponse response, Context ctx) throws Exception {
		String path = request.getRequestURI();

		System.out.println(request.getRequestURL());
		ActionContainer actionContainer = ActionContainer.getInstance();
		ActionConfig action = actionContainer.getAction(path);
		String className = action.getClassName();
		String method = action.getMethod();

		Map<String, Object> input = makeRequestToMap(request);

		String resultName = executeAction(className, method, input, ctx);

		String resultTemp = action.getResult(resultName);

		Template temp = Velocity.getTemplate(resultTemp);
		

		return temp;
	}

	private String executeAction(String className, String method,
			Map<String, Object> input, Context ctx) throws Exception {
		Map<String, Object> output = new HashMap<String, Object>();
		Class action = Class.forName(className);
		Action newAction = (Action) action.newInstance();
		String result = newAction.execute(input, output);
		Iterator<String> i = output.keySet().iterator();
		while (i.hasNext()) {
			String key = i.next();
			ctx.put(key, output.get(key));
		}
		return result;
	}
	
	private void setEncodeType(ServletConfig config){
		String encode = config.getServletContext().getInitParameter(TAG_ENCODE);
		this.setEncode(encode);		
	}

	private Map makeRequestToMap(HttpServletRequest request) {
		Map tempMap = request.getParameterMap();
		Map<String, Object> result = new HashMap<String, Object>();
		Iterator<String> i = tempMap.keySet().iterator();
		while (i.hasNext()) {
			String key = i.next();
			String[] values = (String[]) tempMap.get(key);
			if (values.length == 1) {
				result.put(key, values[0]);
			} else {
				result.put(key, values);
			}
		}
		return result;
	}

	private void webConfigInit(ServletConfig config) {

		String mapfile = config.getInitParameter(TAG_WEB_CONFIG);

		ActionContainer.init(realPath + "/" + mapfile);
		log.debug("init webconfig complete.");

	}
}
